/**
 * @author attilax 老哇的爪子
	@since  2014-6-17 上午09:28:00$
 */
package com.attilax.sso;

import java.io.File;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.attilax.io.pathx;
import com.attilax.net.cookieUtil;
import com.attilax.seo.AppIco;
import com.attilax.util.PropX;
import com.attilax.util.randomx;
import com.attilax.util.tryX;
//import com.focustar.interceptor.promise;






import m.secury.callbackItfs;

/**
 * @author attilax 老哇的爪子
 * @since 2014-6-17 上午09:28:00$
 */
public class LoginX {

	public LoginX() {
		appId = getCfg("appId");

	}

	private String getCfg(String cfgName) {

		String v = System.getProperty(cfgName);
		if (v == null) {
			try {
				PropX px = new PropX(pathx.webAppPath() + "/cfg.txt");
				v = px.getProperty(cfgName);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		if (v == null)
			v=getAppIDinOneday();
		return v;
	}
	/**
	 * YYYY_MM dd
	 * @return
	 */
	 public   String getAppIDinOneday( ) {
		 java.util.Date   dt   =   new   java.util.Date(System.currentTimeMillis());  
	     SimpleDateFormat   fmt   =   new   SimpleDateFormat("MMdd");  
	     String   fileName=   fmt.format(dt);  
	     return "date"+fileName;
	 }

	String appId;
	private String mod;

	/**
	 * @author attilax 老哇的爪子
	 * @since 2014-6-17 上午09:28:00$
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// attilax 老哇的爪子 上午09:28:00 2014-6-17
		System.out.println(System.getProperty("appId"));

	}

	public boolean isLogined(HttpServletRequest req) {
		String uid = cookieUtil.getCookie(appId + "_uid_"+mod, req);
		if (uid.equals(""))
			return false;
		else
			return true;

	}

	// attilax 老哇的爪子 上午09:28:00 2014-6-17

	/**
	 * @author attilax 老哇的爪子
	 * @since 2014-6-17 上午09:30:39$
	 * 
	 * @param callbackItfs
	 */
	@Deprecated
	public void setToken(callbackItfs callbackItfs) {
		// attilax 老哇的爪子 上午09:30:39 2014-6-17
		callbackItfs.callMethod("");

	}

	/**
	 * @author attilax 老哇的爪子
	 * @since 2014-6-17 上午09:37:37$
	 * 
	 * @param name
	 * @param id
	 * @param httpServletResponse
	 */
	public void setToken(final String name, final String uid,
			final HttpServletResponse httpServletResponse) {
		// attilax 老哇的爪子 上午09:37:37 2014-6-17
		new tryX<Object>() {

			@Override
			public Object item(Object t) throws Exception {
				// attilax 老哇的爪子 上午09:50:43 2014-6-17
				cookieUtil.add(appId + "_uname_"+mod, name, httpServletResponse);
				cookieUtil.add(appId + "_uid_"+mod, uid.toString(),
						httpServletResponse);
				// cookieUtil.add("uname",name,httpServletResponse);
				// cookieUtil.add(appId+"_uid",uid.toString(),httpServletResponse);
				return null;
			}
		}.$("");

	}

	/**
	 * @author attilax 老哇的爪子
	 * @since 2014-6-17 上午09:57:08$
	 * 
	 * @param object
	 */
	public static void exTok(final com.attilax.promise object) {
		// attilax 老哇的爪子 上午09:57:08 2014-6-17
		new tryX<Object>() {

			@Override
			public Object item(Object t) throws Exception {
				// attilax 老哇的爪子 上午10:45:39 2014-6-17
				object.$();
				return null;
			}
		}.$("");

	}

	/**
	 * @author attilax 老哇的爪子
	 * @since 2014-6-17 上午10:13:13$
	 * 
	 * @return
	 */
	public   String getuid(HttpServletRequest request) {
		// attilax 老哇的爪子 上午10:13:13 2014-6-17

		return cookieUtil.getCookie(appId+"_uid_"+this.mod, request);
	}

	public   String getuname(HttpServletRequest request) {
		// attilax 老哇的爪子 上午10:13:13 2014-6-17

		return cookieUtil.getCookie(appId+"_uname_"+this.mod, request);
	}

	/**
	 * 
	 * @author attilax 老哇的爪子
	 * @since 2014-6-24 下午6:35:13$
	 */
	public void loginOut(final HttpServletResponse httpServletResponse) {
		// attilax 老哇的爪子 下午6:35:13 2014-6-24
		new tryX<Object>() {

			@Override
			public Object item(Object t) throws Exception {
				// attilax 老哇的爪子 上午09:50:43 2014-6-17
				cookieUtil.add(appId + "_uname_"+mod, "", httpServletResponse);
				cookieUtil.add(appId + "_uid_"+mod, "", httpServletResponse);
				return null;
			}
		}.$("");

	}

	/**
	 * @return
	 * @author attilax 老哇的爪子
	 * @since 2014-6-24 下午6:47:38$
	 */
	public static boolean isLocal() {
		// attilax 老哇的爪子 下午6:47:38 2014-6-24

		return new com.attilax.tryX<Boolean>() {

			@Override
			public Boolean $$(Object t) throws Exception {
				// attilax 老哇的爪子 下午6:48:34 2014-6-24
				// if()
				return new File("c:\\localFlag").exists();
			}
		}.$(false);
	}

	public void setModule(String mod) {
	this.mod=mod;
		
	}
}

// attilax 老哇的爪子